var app=getApp();
Page({
  onShow:function(){
    // 生命周期函数--监听页面初次渲染完成
    wx.setNavigationBarTitle({
       title: '开心一刻',
    })
  },
  data:{
    currentSelectedNavBar:"0",
    result:[],
    index:1,
    // 经典笑话的数据
    resultClascial:[],
    maxtime:"",
    // 0代表全部加载成功  1代表热门失败  2代表经典失败
    isFaile:0,
  },
  // 点击热门笑话
  clickHotFun:function(e){
    var that = this
    that.setData({
        currentSelectedNavBar:0,
    })
  },
  // 点击经典笑话
  clickClasscialFun:function(e){
    var that = this
    that.setData({
        currentSelectedNavBar:1,
    })
  },
  onLoad:function(){
    this.netRequest()
    this.netForClasscialFun()
  },

  // 热门笑话网络请求
  netRequest:function(){
    var that = this;
    app.showHud()
    wx.request({
      url: 'http://m2.qiushibaike.com/article/list/text?count=30&readarticles=%5B114089150%2C114089598%5D&',
      data: {
        page:that.data.index,
        AdID:'14491961669474FDE30D21',
      },
      header: {
      },
      success: function(res) {
        console.log("=====success")
        console.log(res.data.items)
        that.setData({
          result:res.data.items,
          isFaile:0
        })
      },
      fail: function (res) {
        console.log("failed")
        that.setData({
          isFaile:1
        })
      }
    })
  },
  // 热门笑话下拉刷新
  upper: function () {
    var that = this;
    wx.showNavigationBarLoading()
    that.setData({
       index:1,
       isfooter:false,
    }),
    this.netRequest();
    console.log("upper");
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000);
  },
  // 热门笑话上拉加载
  lower: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
       index:that.data.index+1,
       isfooter:true,
    }),
    console.log(that.data.index);
    setTimeout(function(){wx.hideNavigationBarLoading();that.netRequestMore();}, 1000);
    console.log("lower")
  },
  netRequestMore:function(){
    var that = this;
    app.showHud()
    wx.request({
      url: 'http://m2.qiushibaike.com/article/list/text?count=30&readarticles=%5B114089150%2C114089598%5D&',
      data: {
        page:that.data.index,
        AdID:'14491961669474FDE30D21',
      },
      header: {
      },
      success: function(res) {
        console.log("=====success")
        console.log(res.data.items)
        that.setData({
          result:that.data.result.concat(res.data.items),
        })
      },
      fail: function (res) {
        console.log("failed")
      }
    })
  },

  // 经典笑话第一次加载
  netForClasscialFun:function(){
    var that = this;
    app.showHud()
    wx.request({
      url: 'http://api.budejie.com/api/api_open.php',
      data: {
        a:'list',
        c:'data',
        type:29,
        maxtime:that.data.maxtime,
      },
      header: {
      },
      success: function(res) {
        console.log("classcial=====success")
        console.log(res.data.list)
        that.setData({
          resultClascial:res.data.list,
          maxtime:res.data.info.maxtime,
          isFaile:0
        })
      },
      fail: function (res) {
        console.log("failed")
        that.setData({
          isFaile:2
        })
      }
    })
  },
  // 经典笑话上拉加载
  lowerClasscial:function(){
     var that = this;
     app.showHud()
     wx.request({
      url: 'http://api.budejie.com/api/api_open.php',
      data: {
        a:'list',
        c:'data',
        type:29,
        maxtime:that.data.maxtime,
      },
      
      header: {
      },
      
      success: function(res) {
        console.log("classcial=====success===more")
        console.log(that.data.maxtime)
        that.setData({
          resultClascial:that.data.resultClascial.concat(res.data.list),
          maxtime:res.data.info.maxtime
        })
      },
      fail: function (res) {
        console.log("failed")
      }
    })
  },
  // 经典笑话下拉刷新
  upperClasscial:function(){
    var that=this
     that.setData({
        maxtime:"",
     })
     that.netForClasscialFun()
  },

  // 网络请求失败
  loadAgain:function(){
    isFaile==1?this.netRequest():this.netForClasscialFun()
  }
})